﻿using System;
using System.Collections.Generic;
using ApplicationGenerator.BusinessModule.MetaElements;
using ApplicationGenerator.DataAccess;
using ApplicationGenerator.BusinessModule.Interfaces;

namespace ApplicationGenerator.Versions.BusinessLogic
{
    /// <summary>
    /// Klasa dostępu do elementów wersji dla danego typu elementu
    /// </summary>
    public class VersionDataAccess
    {
        private VersionElement _versionElement;

        public VersionDataAccess(VersionElement versionElement)
        {
            _versionElement = versionElement;
        }

        public IList<VersionInfo> GetVersions()
        {
            List<List<object>> result = null;

            switch (_versionElement)
            {
                case VersionElement.Form:
                    result = DesignerDataBaseManager.Instance.GetVersionInfoValues(Policy.Form);
                    break;
                case VersionElement.List:
                    result = DesignerDataBaseManager.Instance.GetVersionInfoValues(Policy.List);
                    break;
                case VersionElement.Workflow:
                    result = DesignerDataBaseManager.Instance.GetVersionInfoValues(Policy.Workflow);
                    break;
                case VersionElement.Model:
                    {
                        return GetModels();
                    }
            }

            IList<VersionInfo> versions = new List<VersionInfo>();
            try
            {
                foreach (List<object> pair in result)
                {
                    VersionInfo vinfo = new VersionInfo();
                    vinfo.Version = (Guid)pair[0];
                    vinfo.Name = (string)pair[1];
                    versions.Add(vinfo);
                }
            }
            catch (Exception)
            {
            }

            return versions;
        }

        private static IList<VersionInfo> GetModels()
        {
            IList<VersionInfo> models = new List<VersionInfo>();

            foreach (var item in MetaModelManager.Instance.MetaModelCollection)
            {
                VersionInfo modelInfo = new VersionInfo();
                modelInfo.Name = (item as IMetaModel).ModelName;
                modelInfo.Version = Guid.Empty;
                models.Add(modelInfo);
            }

            return models;
        }
    }
}
